from ise_api_0_login_info import username, password, url_prefix, headers
from ise_api_3_group import get_group_detail_by_name
import requests
from requests.auth import HTTPBasicAuth
import json
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


create_user_url = 'config/internaluser'


# 創建用戶
def create_user(user, password, group=None):
    if group:
        user_data_dict = {'name': user,
                          'identityGroups': get_group_detail_by_name(group).get('id'),
                          'password': password}
    else:
        user_data_dict = {'name': user,
                          'password': password}

    r = requests.post(url_prefix + create_user_url,
                      auth=HTTPBasicAuth(username, password),
                      headers=headers,
                      json={"InternalUser": user_data_dict},
                      verify=False)
    try:
        return r.json()
    except json.decoder.JSONDecodeError:
        return None


if __name__ == '__main__':
    print(create_user('user5', 'Cisc0123', group='Employee'))